package subject.tengxun.middle;

import java.util.*;

/**
 * 功能：
 * 作者：yedou
 * 日期：2025/3/12 20:34
 */
public class groupAnagrams {

    public static void main(String[] args) {
        // 集合对dgg ddg 是一样的其实不一样
        String[] strs = {"dgg", "ddg", "eat", "tea", "tan", "ate", "nat", "bat"};
//        HashMap<Set<Character>, List<String>> hashMap = new HashMap<>();
        HashMap<Map<Character, Integer>, List<String>> hashMap = new HashMap<>();
        // 遍历数组
        for (String str : strs) {
            // 统计字符频率
            Map<Character, Integer> hashMap1 = new HashMap<>();
            for (int i = 0; i < str.length(); i++) {
                char ch = str.charAt(i);
                hashMap1.put(ch, hashMap1.getOrDefault(ch, 0) + 1);
            }
            // 检查是否已存在相同的字符频率
            if (!hashMap.containsKey(hashMap1)) {
                List<String> arrayList = new ArrayList<>();
                arrayList.add(str);
                hashMap.put(hashMap1, arrayList);
            } else {
                List<String> list = hashMap.get(hashMap1);
                list.add(str);
            }
        }
        // 获取value
        List<List<String>> arrayList = new ArrayList<>();
        Set<Map.Entry<Map<Character, Integer>, List<String>>> entries = hashMap.entrySet();
        for (Map.Entry<Map<Character, Integer>, List<String>> entry : entries) {
            arrayList.add(entry.getValue());
        }

        System.out.println(arrayList);
    }
}